﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirmaTransportowa
{
    public partial class CashierPanelPackageDetails : Form
    {
        CashierPanel myParentForm;

        public delegate void TestEventHandler(object sender, EventArgs e);
        public Package currentPackage = null;
        public Package defaultPackage;

        public event Action SenderIndexChanged;
        public event Action RecipientIndexChanged;
        public event Action ResetClicked;
        public event Action SaveClicked;

        public CashierPanelPackageDetails(CashierPanel myParent,Package package=null)
        {
            InitializeComponent();
            this.myParentForm = myParent;
            
            listBox1.SelectedIndex = -1;
            listBox2.SelectedIndex = -1;
                        
            listBox1.SelectedIndexChanged += OnSenderIndexChanged;
            listBox2.SelectedIndexChanged += OnRecipientIndexChanged;
            button1.Click += OnButtonResetClicked;
            button2.Click += OnButtonSaveClicked;

            currentPackage = package;

            if (package!=null)
            {
                fillForm();
            }
        }

        private void clearErrorsLabels()
        {
            foreach (Label label in this.Controls.OfType<Label>())
            {
                if(label.Name.Contains("Error")){
                    label.Visible=false;
                }
            }
        }

        private void fillForm()
        {
            tbSenderName.Text = currentPackage.sender.name;
            tbSenderSurname.Text = currentPackage.sender.surname;
            tbSenderPostcode.Text = currentPackage.sender.postcode;
            tbSenderCity.Text = currentPackage.sender.city;
            tbSenderStreet.Text = currentPackage.sender.street;
            tbSenderHouseNumber.Text = currentPackage.sender.house_number;
            tbSenderPhone.Text = currentPackage.sender.phone;

            tbRecipientName.Text = currentPackage.recipient.name;
            tbRecipientSurname.Text = currentPackage.recipient.surname;
            tbRecipientPostcode.Text = currentPackage.recipient.postcode;
            tbRecipientCity.Text = currentPackage.recipient.city;
            tbRecipientStreet.Text = currentPackage.recipient.street;
            tbRecipientHouseNumber.Text = currentPackage.recipient.house_number;
            tbRecipientPhone.Text = currentPackage.recipient.phone;

            tbPackageWeight.Text = currentPackage.weight.ToString();
        }

        public void fillSender(Sender objSender)
        {
            if (objSender != null)
            {
                tbSenderName.Text = objSender.name;
                tbSenderSurname.Text = objSender.surname;
                tbSenderPostcode.Text = objSender.postcode;
                tbSenderCity.Text = objSender.city;
                tbSenderStreet.Text = objSender.street;
                tbSenderHouseNumber.Text = objSender.house_number;
                tbSenderPhone.Text = objSender.phone;
            }
        }

        public void fillRecipient(Recipient objRecipient){
            if (objRecipient != null)
            {
                tbRecipientName.Text = objRecipient.name;
                tbRecipientSurname.Text = objRecipient.surname;
                tbRecipientPostcode.Text = objRecipient.postcode;
                tbRecipientCity.Text = objRecipient.city;
                tbRecipientStreet.Text = objRecipient.street;
                tbRecipientHouseNumber.Text = objRecipient.house_number;
                tbRecipientPhone.Text = objRecipient.phone;
            }
        }

        private void CashierPanelPackageDetails_Load(object sender, EventArgs e)
        {
            listBox1.DisplayMember = "displayName";
            listBox1.ValueMember = "id";

            listBox2.DisplayMember = "displayName";
            listBox2.ValueMember = "id";

        }

        private void OnRecipientIndexChanged(object sender, EventArgs e)
        {
            if (this.RecipientIndexChanged != null)
            {
                this.RecipientIndexChanged();
            }
        }

        private void OnSenderIndexChanged(object sender, EventArgs e)
        {
            if (this.SenderIndexChanged != null)
            {
                this.SenderIndexChanged();
            }
        }

        private void OnButtonResetClicked(object sender, EventArgs e)
        {
            if (this.ResetClicked != null)
            {
                this.ResetClicked();
            }
        }

        private void OnButtonSaveClicked(object sender, EventArgs e)
        {
            if (this.SaveClicked != null)
            {
                this.SaveClicked();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void CashierPanelPackageDetails_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 4);
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                if(currentPackage==null)
                    HelpUtils.showHelp(this, 4);
                else
                    HelpUtils.showHelp(this, 5);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
